package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GreTreePatrolLogFileLogic;
import cn.topevery.garden.dal.logic.GreTreePatrolLogLogic;
import cn.topevery.garden.entity.dto.GreTreePatrolLogDTO;
import cn.topevery.garden.entity.po.GreTreePatrolLog;
import cn.topevery.garden.entity.po.GreTreePatrolLogFile;
import cn.topevery.garden.entity.query.GreTreePatrolLogQuery;
import cn.topevery.garden.entity.vo.GreTreePatrolLogVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 巡查记录表
 *
 * @author code-generate
 * @date 2022-02-27
 */
@Api(tags = "巡查记录表")
@RestController
@RequestMapping("/greTreePatrolLog")
public class GreTreePatrolLogController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreTreePatrolLogLogic greTreePatrolLogLogic;

    @Autowired
    GreTreePatrolLogFileLogic greTreePatrolLogFileLogic;

    /**
     * 获取巡查记录表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取巡查记录表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GreTreePatrolLogVO>> listPage(@RequestBody GreTreePatrolLogQuery query) {
        return JsonResult.ok(greTreePatrolLogLogic.listPage(query));
    }
    @ApiOperation(value = "获取巡查记录表页列表")
    @PostMapping("/getList")
    public JsonResult<DataPager<GreTreePatrolLogVO>> getList(@RequestBody GreTreePatrolLogQuery query) {
        DataPager<GreTreePatrolLogVO> res = greTreePatrolLogLogic.listPage(query);
        for(GreTreePatrolLogVO tempMap : res.records){
            List<GreTreePatrolLogFile> tempList = greTreePatrolLogFileLogic.getList(tempMap.getId());
            tempMap.setFileList(tempList);
        }
        return JsonResult.ok(greTreePatrolLogLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 巡查记录表信息")
    @PostMapping("/save")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody GreTreePatrolLogDTO dto) {
        String userId = getUser().id.toString();
        GreTreePatrolLog greTreePatrolLog = mapper.map(dto, GreTreePatrolLog.class);
        greTreePatrolLogLogic.saveOrUpdate(userId,greTreePatrolLog);
        if(greTreePatrolLog.getFileList() != null){
            greTreePatrolLogFileLogic.deleteByPid(greTreePatrolLog.getId());
            for(GreTreePatrolLogFile greTreePatrolLogFile: greTreePatrolLog.getFileList()){
                greTreePatrolLogFile.setMainId(greTreePatrolLog.getId());
                greTreePatrolLogFile.setDbStatus(false);
                greTreePatrolLogFileLogic.saveOrUpdate(userId, greTreePatrolLogFile);
            }
        }
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
//    @GetMapping("/getInfoById")
    @GetMapping("/getById")
    public JsonResult<GreTreePatrolLogVO> getInfoById(@RequestParam Integer id) {
        GreTreePatrolLog entity = greTreePatrolLogLogic.getEntity(id);
        List<GreTreePatrolLogFile> list = greTreePatrolLogFileLogic.getList(entity.getId());
        entity.setFileList(list);
        GreTreePatrolLogVO vo= mapper.map(entity, GreTreePatrolLogVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
//    @PostMapping("/deleteBatchById")
    @GetMapping("/delete")
    public JsonResult<Boolean> deleteBatchById(@RequestParam Integer[] ids) {
        greTreePatrolLogLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出巡查记录表信息")
    @GetMapping("/export")
    public void export(GreTreePatrolLogQuery query, HttpServletResponse response) {
        List<GreTreePatrolLogVO> list = greTreePatrolLogLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "巡查记录表", "sheet1", GreTreePatrolLogVO.class, "巡查记录表", response);
    }

}
